#include <EEPROM.h>
byte input;
volatile unsigned int tunda = 20;
volatile unsigned int temp;
volatile unsigned long suhu;
void setup(){
   analogReference(INTERNAL);
   pinMode(13,OUTPUT);
   pinMode(8, OUTPUT);
   pinMode(5,OUTPUT);
   Serial.begin(57600);
   temp = EEPROM.read(0);
   if(temp == 255){EEPROM.write(0,tunda);}
   temp |= EEPROM.read(1) << 8;
   tunda = temp;
   temp = 0;
  
}
//48-57
void loop(){
  if(Serial.available()){
    input = Serial.read();
    if(input <= 57 && input >= 48){
      temp = 10 * temp + (input - 48);
    }else if(input == 13 && temp != 0){
      tunda = constrain(temp, 2, 700);
      temp = EEPROM.read(0) | (EEPROM.read(1) << 8);
      
      if(tunda != temp){EEPROM.write(0,tunda);EEPROM.write(1,(tunda & 0xFF00)>>8);}
      temp = 0;
      Serial.print("ONt = ");
      Serial.print(tunda,DEC);
      Serial.println(" uS");
      analogWrite(5,tunda);
    }else if(input == 'a'){
      Serial.print("ONt = ");
      Serial.print(tunda,DEC);
      Serial.println(" uS");
      Serial.print("Suhu = ");
      Serial.println(suhu,DEC);
    }
  }
  
  suhu =  (long) (analogRead(0) * 110ul) / 1024;
  //digitalWrite(13,HIGH);
  digitalWrite(8,HIGH);
  //suhu  *= 110u /1024;
  delayMicroseconds(tunda);
  //digitalWrite(13,LOW);
  digitalWrite(8,LOW);
  //delayMicroseconds(1000 - tunda);
  delayMicroseconds(590);
  //delay(1);
}
